<?php


namespace logic\cyf;


use frontend\models\cyf\Sign;
use servers\RedisServer;

class SignLogic
{
    /**
     * 签到
     * @param $uid
     * @throws \Exception
     */
    public static function sign($params)
    {
        $redis = RedisServer::entrance();
        $sign = $redis->signAdd($params['uid'],$params['date']);//记录数据到redis
        if(!$sign) throw new \Exception('已经签到过了');
        $score = self::getIntegral($redis,$params['uid'],$params['date']);//获取积分
        $data = [
            'uid'=>$params['uid'],
            'sign_time'=>time(),
            'score'=>$score,
            'create_time'=>date('Y-m-d H:i:s')
        ];
        $res = Sign::create($data);
        if(!$res) throw new \Exception('签到失败');
        return true;
    }

    /**
     * 连续签到送积分
     * @param $redis
     * @param $uid
     * @param string $day
     * @return int
     */
    public static function getIntegral($redis,$uid,$day='')
    {
        $chainDay = $redis->getField($uid,$day);
        switch ($chainDay){
            case 1 : $integ = 10;break;
            case 2 : $integ = 20;break;
            case 3 : $integ = 30;break;
            case 4 : $integ = 40;break;
            case 5 : $integ = 50;break;
            case 6 : $integ = 60;break;
            case 7<=$chainDay : $integ = 70;break;
        }
        return $integ;
    }
}